package com.xayy.health.mgt.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.Method;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.HttpUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.alibaba.fastjson.JSON;
import com.xayy.health.core.config.param.ExternalParam;
import com.xayy.health.core.exception.HealthException;
import com.xayy.health.mgt.model.SatelliteParam;
import com.xayy.health.mgt.model.vo.tupu.EquipmentResponseVo;
import com.xayy.health.mgt.model.vo.tupu.NodeVo;
import com.xayy.health.mgt.service.SatelliteParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangxa
 */
@Service
public class SatelliteParamServiceImpl extends BaseServiceImpl<SatelliteParam> implements SatelliteParamService {

    @Autowired
    private ExternalParam externalParam;

    /**
     * 获取卫星信息
     *
     * @param satelliteId 卫星id
     * @return 结果日志
     */
    @Override
    public List<SatelliteParam> selectByOther(Long satelliteId) {
        List<SatelliteParam> satelliteParams = new ArrayList<>();
        Map<String, Object> xing = new HashMap<>(3);
        xing.put("filterFlag", true);
        xing.put("id", satelliteId);
        xing.put("labelName", "星");
        HttpResponse getResponse = HttpUtil.createRequest(Method.GET, externalParam.satSysUrl).form(xing).execute();
        if (getResponse.getStatus() != HttpStatus.HTTP_OK) {
            throw new HealthException("调用星-分系统接口异常");
        }
        Object data = JSON.parseObject(getResponse.body()).get("data");
        EquipmentResponseVo tupu = JSON.parseObject(data.toString(), EquipmentResponseVo.class);
        List<NodeVo> nodes = tupu.getNodes();
        for (NodeVo nodeVo : nodes) {
            if ("分系统".equals(nodeVo.getLabel())) {
                Map<String, Object> fxt = new HashMap<>(3);
                fxt.put("filterFlag", true);
                fxt.put("id", nodeVo.getId());
                fxt.put("labelName", "分系统");
                HttpResponse getResponse1 =
                    HttpUtil.createRequest(Method.GET, externalParam.sysComUrl).form(fxt).execute();
                if (getResponse1.getStatus() != 200) {
                    throw new HealthException("调用分系统-设备接口异常");
                }
                Object data1 = JSON.parseObject(getResponse1.body()).get("data");
                if (BeanUtil.isEmpty(data1)){
                    continue;
                }
                EquipmentResponseVo responseVo1 = JSON.parseObject(data1.toString(), EquipmentResponseVo.class);
                List<NodeVo> nodes1 = responseVo1.getNodes();
                for (NodeVo nodeVo1 : nodes1) {
                    if ("设备".equals(nodeVo1.getLabel())) {
                        Map<String, Object> sb = new HashMap<>(3);
                        sb.put("filterFlag", false);
                        sb.put("id", nodeVo1.getId());
                        sb.put("labelName", "设备");
                        HttpResponse getResponse2 =
                            HttpUtil.createRequest(Method.GET, externalParam.comArgUrl).form(sb).execute();
                        if (getResponse2.getStatus() != 200) {
                            throw new HealthException("调用设备-参数接口异常");
                        }
                        Object data2 = JSON.parseObject(getResponse2.body()).get("data");
                        if (BeanUtil.isEmpty(data2)){
                            continue;
                        }
                        EquipmentResponseVo responseVo2 = JSON.parseObject(data2.toString(), EquipmentResponseVo.class);
                        List<NodeVo> nodes2 = responseVo2.getNodes();
                        for (NodeVo nodeVo2 : nodes2) {
                            if ("参数".equals(nodeVo2.getLabel())) {
                                SatelliteParam satelliteParam = new SatelliteParam();
                                satelliteParam.setParamName(nodeVo2.getProperties().getName());
                                satelliteParam.setTelemetryCode(nodeVo2.getProperties().getCsdh());
                                satelliteParams.add(satelliteParam);
                            }
                        }
                    }
                }
            }
        }
        return satelliteParams;
    }
}
